package net.didactylos.helpline.action;


import java.util.*;

import org.hibernate.*;

import net.didactylos.helpline.data.*;
import net.didactylos.helpline.data.Queue;
import net.didactylos.helpline.stripes.bean.HelplineActionBean;
import net.didactylos.helpline.stripes.security.*;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.validation.*;
import net.didactylos.helpline.data.Message;

/**
 * Specify message, retrieve conversation, contact, conversations, messages.
 * Specify contact, retrieve conversations, messages.
 * Specify conversation, retrieve contact, conversations, messages.
 * 
 * Simplify things, ignore previous conversations for now.
 * 
 * Get contact from conversation.
 * 
 * @author david
 *
 */
public class ConversationAction extends HelplineActionBean
{
	private Message message;
	
	private Contact contact;
	
	private Conversation conversation;
	
	private List<Message> messages;
	
	private Queue queue;
	
	private Message draft;
	
//	private List<Conversation> conversations;

	public Message getMessage()
	{
		// May be null.
		return message;
	}

	public void setMessage(Message message)
	{
		this.message = message;
	}

	public Contact getContact()
	{
		if (contact == null)
		{
			contact = getConversation().getContact();
		}
		return contact;
	}

	public void setContact(Contact contact)
	{
		this.contact = contact;
	}

	public Conversation getConversation()
	{
		if ((conversation == null) && (message != null))
		{
			conversation = message.getConversation();
		}
		// May be null.
		return conversation;
	}

	public void setConversation(Conversation conversation)
	{
		this.conversation = conversation;
	}

	public List<Conversation> getConversations()
	{
		return contact.getConversations();
	}

	@SuppressWarnings("unchecked")
	public List<Message> getMessages()
	{
		if (messages == null)
		{
			messages = (List<Message>) getContext().getSession()
			.createQuery("from Message as msg where msg.conversation = ? " +
					"and msg.status <> ?")
			.setParameter(0, conversation)
			.setInteger(1, MessageStatus.DRAFT.ordinal())
			.list();
		}
		return messages;
	}

	public Queue getQueue()
	{
		return queue;
	}

	public void setQueue(Queue queue)
	{
		this.queue = queue;
	}

	public Message getDraft()
	{
		if (draft == null)
		{
			draft = (Message) getContext().getSession()
			.createQuery("from Message as msg where msg.inReplyTo = ? " +
			"and msg.status = ?")
			.setParameter(0, message)
			.setInteger(1, MessageStatus.DRAFT.ordinal())
			.setMaxResults(1)
			.uniqueResult();
			
			if (draft == null)
			{
				draft = new Message();
			}
		}
		return draft;
	}

	public void setDraft(Message draft)
	{
		this.draft = draft;
	}

	@DontValidate
	@DefaultHandler
	@Secured(Capabilities.SIGN_IN)
	public Resolution view()
	{
		return new ForwardResolution("conversation.jspx");
	}
	
	@DontValidate
	@Secured(Capabilities.SIGN_IN)
	public Resolution reply()
	{
		// Check for conversation belonging to this user
		conversation = (Conversation) getContext().getSession()
		.createQuery("from Conversation as conv where conv.owner = ?")
		.setParameter(0, getContext().getUser())
		.uniqueResult();
		
		if (conversation == null)
		{
			message = (Message) getContext().getSession()
			.createQuery("from Message as msg where msg.queue = ? " +
					"and msg.conversation.owner is null")
			.setMaxResults(1)
			.setParameter(0, queue)
			.uniqueResult();
			
			if (message == null)
			{
				getContext().getMessages().add(new LocalizableMessage("replyNothingMessage"));
				return new RedirectResolution("/");
			}
			conversation = message.getConversation();
			conversation.setOwner(getContext().getUser());
			getContext().getSession().update(conversation);
		}
		else
		{
			message = (Message) getContext().getSession()
			.createQuery("from Message as msg where msg.conversation = ? " +
					"and msg.status = ?")
			.setMaxResults(1)
			.setParameter(0, conversation)
			.setInteger(1, MessageStatus.NEW.ordinal())
			.uniqueResult();
		}
		
		return new ForwardResolution("conversation.jspx");
	}

}
